$(function () {
    var params = Params();
    num();
    plus();
    less();
    getData();
    addShopCar();

    function num() {
        $('#num').keyup(function () {
            var val = $(this).val();
            var regExp = /^[0-9]+$/;
            if (!regExp.test(val)) {
                $(this).val(1);
            }
        });
    }

    function plus() {
        $('#plus').tap(function () {
            var num = $('#num');
            num.val((num.val() * 1) + 1);
        });

    }

    function less() {
        $('#less').tap(function () {
            var num = $('#num');
            if (num.val() <= 1) {
                num.val(1);
            } else {
                num.val(num.val() - 1);
            }
        });
    }

    function getData() {
        var option = {
            url:HOST+ '/admin/pro_details.php',
            type: 'get',
            data: {
                getType: 'proData',
                id: params['id']
            }
        };
        request(option).then(function (data) {
            $('#img').attr('src', data[0]['img_src']);
            $('#title').html(data[0]['title']);
            $('#price').html(data[0]['price']);
            $('#sales').html(data[0]['sales']);
        });
    }

    function Params() {
        var result = {}, hash = location.hash, search = location.search;
        result.docName = decodeURIComponent(location.pathname).match(/\/([^\/]+)\.html$/)[1];//提取文件名
        if (hash) {
            result.hash = hash.replace(/^\#/, ''); //提前锚点
        }
        if (search) {
            var arr = search.replace(/^\?/, '').split(/\&/g); //提前所有search参数
            for (var i = 0, n = arr.length; i < n; i++) {
                var key = arr[i].split(/\=/)[0];
                result[key] = decodeURIComponent(arr[i].split(/\=/)[1]);
            }
        }
        return result;
    }

    function addShopCar() {
        $('#add').tap(function () {
            checkLoginStatus();
            var option = {
                url: HOST+'../admin/pro_details.php',
                type: 'post',
                data: {
                    postType: 'addShopCar',
                    id: params['id'],
                    user: LsyCookie.getItem('loginUserName'),
                    num: $('#num').val()
                }
            };
            request(option).then(function (data) {
            });
            window.location.href = './shopCar.html';
        });
    }
});